package com.github.catvod.spider;

import com.github.catvod.bean.Class;
import com.github.catvod.bean.Result;
import com.github.catvod.bean.Vod;
import com.github.catvod.crawler.Spider;
import com.github.catvod.net.OkHttp;
import com.github.catvod.net.OkResult;
import com.github.catvod.utils.Json;
import com.github.catvod.utils.Util;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 熊猫视频
 */
public class XiongMao extends Spider {
    // 网站访问网址
    private static String siteUrl="https://www.9rxm.com/";

    // 导航请求
    private String dataInit="https://xmjk.kphm88.com/getDataInit";

    // 视频分类请求
    private String forward="https://xmjk.kphm88.com/forward";
    private String imgBaseUrl="https://x-hweu5.sxpgb.com";
    private String videoBaseUrl="https://server16.8b0o0g.com";

    private Map<String, String> getHeader() {
        Map<String, String> header = new HashMap<>();
        header.put("user-Agent", Util.CHROME);
        return header;
    }


    @Override
    public String homeContent(boolean filter) throws Exception {
        // 进行获取导航栏
        HashMap<String, String> params = new HashMap<>();
        params.put("age", "31");
        params.put("city", "New York");
        params.put("name", "John");
        OkResult post = OkHttp.post(dataInit,Json.toJson(params), getHeader());
        List<Class> ClassList=new ArrayList<>();
        if (post.getCode() == 200){
            JSONObject result_json = new JSONObject(post.getBody());
            JSONArray jsonArray = result_json.getJSONObject("data").getJSONArray("menu0ListMap");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONArray arr = jsonArray.getJSONObject(i).getJSONArray("menu2List");
                for (int j = 0; j <arr.length() ; j++) {
                    String name = arr.getJSONObject(j).getString("typeName2");
                    String typeId = arr.getJSONObject(j).getString("typeId2");
                    String typeMid = arr.getJSONObject(j).getString("typeMid2");
                    ClassList.add(new Class("typeId="+typeId+"&typeMid="+typeMid,name));
                }
                if (i>=2) break;
            }
            List<Vod> vodList = new ArrayList<>();
            return Result.string(ClassList, vodList);
        }

        return "";
    }

    @Override
    public String categoryContent(String tid, String pg, boolean filter, HashMap<String, String> extend) throws Exception {
        System.out.println(tid);
        String[] split = tid.split("&");
        String typeId = split[0].split("=")[1];
        String typeMid = split[1].split("=")[1];
        System.out.println("id:"+typeId+" mid:"+typeMid);
        HashMap<String, String> params = new HashMap<>();
        params.put("RecordsPage", "20");
        params.put("command", "WEB_GET_INFO");
        params.put("content", "");
        params.put("languageType", "CN");
        params.put("pageNumber", pg);
        params.put("typeId", typeId);
        params.put("typeMid", typeMid);
        OkResult post = OkHttp.post(forward,Json.toJson(params), getHeader());
        if (post.getCode() == 200){
            JSONObject result_json = new JSONObject(post.getBody()).getJSONObject("data");
            String count = result_json.getString("count");
            String pageAllNumber = result_json.getString("pageAllNumber");
            String pageNumber = result_json.getString("pageNumber");
            JSONArray vodList = result_json.getJSONArray("resultList");
            List<Vod> list= new ArrayList<>();
            for (int i = 0; i < vodList.length(); i++) {
                String name = vodList.getJSONObject(i).getString("vod_name");
                String pic = vodList.getJSONObject(i).getString("vod_pic");
                String id = vodList.getJSONObject(i).getString("id");
                String type_Mid = vodList.getJSONObject(i).getString("type_Mid");
                String url="typeId="+id+"&typeMid="+type_Mid;
                list.add(new Vod(url,name,pic));
            }
            return Result.string(Integer.parseInt(pageNumber), Integer.parseInt(pageAllNumber), 20, Integer.parseInt(count), list);
        }
        return "";
    }
    @Override
    public String detailContent(List<String> ids) throws Exception {
        String url = ids.get(0);
        String[] split = url.split("&");
        String id = split[0].split("=")[1];
        String mid = split[1].split("=")[1];
        HashMap<String, String> params = new HashMap<>();
        params.put("command", "WEB_GET_INFO_DETAIL");
        params.put("id", id);
        params.put("languageType", "CN");
        params.put("type_Mid", mid);
        OkResult post = OkHttp.post(forward,Json.toJson(params), getHeader());
        if (post.getCode() == 200){
            JSONObject result_json = new JSONObject(post.getBody()).getJSONObject("data").getJSONObject("result");
            String vod_name = result_json.getString("vod_name");
            String vod_pic = imgBaseUrl+result_json.getString("vod_pic");
            String vod_play_url = videoBaseUrl+result_json.getString("vod_url");
            Vod vod=new Vod();
            vod.setVodPlayUrl("播放$"+vod_play_url);
            vod.setVodPic(vod_pic);
            vod.setVodName(vod_name);
            vod.setVodPlayFrom("m3u8");
            return Result.string(vod);
        }
        return "";
    }

    @Override
    public String playerContent(String flag, String id, List<String> vipFlags) throws Exception {
        return Result.get().url(id).header(getHeader()).string();
    }

    @Override
    public String searchContent(String key, boolean quick) throws Exception {
        HashMap<String, String> params = new HashMap<>();
        params.put("RecordsPage", "20");
        params.put("command", "WEB_GET_INFO");
        params.put("content", key);
        params.put("languageType", "CN");
        params.put("pageNumber", "1");
        params.put("type", "1");
        params.put("typeId", "0");
        params.put("typeMid", "1");
        OkResult post = OkHttp.post(forward,Json.toJson(params), getHeader());
        if (post.getCode() == 200){
            JSONObject result_json = new JSONObject(post.getBody()).getJSONObject("data");
            JSONArray jsonArray = result_json.getJSONArray("resultList");
            List<Vod> vodList = new ArrayList<>();
            for (int i = 0; i < jsonArray.length(); i++) {
                String name = jsonArray.getJSONObject(i).getString("vod_name");
                String pic = jsonArray.getJSONObject(i).getString("vod_pic");
                String id = jsonArray.getJSONObject(i).getString("id");
                String type_Mid = jsonArray.getJSONObject(i).getString("type_Mid");
                String url="typeId="+id+"&typeMid="+type_Mid;
                vodList.add(new Vod(url,name,pic));
            }
            return Result.string(vodList);
        }
        return "";
    }
}
